<?php 
$relationsFrom=$customer->customerRelationsFrom;
$relationsTo=$customer->customerRelationsTo;
if(count($relationsFrom)>0){
?>
<div class="relation-heading"><span class="customer-name"><?php echo $customer->getFullName();?></span> is:</div>
<ul>
<?php foreach ($relationsFrom as $relationFrom) {
	?>
	<li><?php echo CHtml::link($relationFrom->customerTo->getFullName(),array('customer/view','id'=>$relationFrom->customerTo->id));?>'s 
	<span class="customer-relation-text"><?php echo $relationFrom->relationText; ?></span>
	<?php if($access){
	echo CHtml::link(CHtml::image(Yii::app()->baseUrl.'/images/icons/cross.png'),array('customerRelation/delete','id'=>$relationFrom->id),
	array('title'=>'Delete the relation','onclick'=>'if(confirm("Delete this relation?")){
	$.post("'.Yii::app()->controller->createUrl('customerRelation/delete',array('id'=>$relationFrom->id)).'",{returnUrl:"'.Yii::app()->controller->createUrl('customer/view',array('id'=>$customer->id)).'",},
	function(){window.location.reload();})
	};return false;'));
	}
	?> 
	
	</li>
<?php } ?>
</ul>
<?php } 
if(count($relationsTo)>0){
?>
<div class="relation-heading"><span class="customer-name"><?php echo $customer->getFullName();?></span>'s</div>
<ul>
<?php foreach ($relationsTo as $relationTo) { ?>
	<li><span class="customer-relation-text"><?php echo $relationTo->relationText; ?></span> is <?php echo CHtml::link($relationTo->customerFrom->getFullName(),array('customer/view','id'=>$relationTo->customerFrom->id));?>
	<?php if($access){
	echo CHtml::link(CHtml::image(Yii::app()->baseUrl.'/images/icons/cross.png'),array('customerRelation/delete','id'=>$relationTo->id,'fromCustomer'=>$customer->id),
	array('title'=>'Delete the relation','onclick'=>'if(confirm("Delete this relation?")){
	$.post("'.Yii::app()->controller->createUrl('customerRelation/delete',array('id'=>$relationTo->id)).'",{returnUrl:"'.Yii::app()->controller->createUrl('customer/view',array('id'=>$customer->id)).'",},
	function(){window.location.reload();})
	};return false;'));
	}
	?> 
	</li>
<?php } ?>
</ul>
<?php }?>
<?php 
if($access){
?>
<div><div class="relation-heading">Add a relation:</div>
<span class="customer-name"><?php echo $customer->getFullName()?></span> is
<?php
	$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
		'name'=>'relation-customer',
    	'sourceUrl'=>Yii::app()->controller->createUrl('customer/quickSearch',array('id'=>$customer->id)),
	// additional javascript options for the autocomplete plugin
      'options'=>array(
          'minLength'=>'2',
'select'=> 'js:function(event,ui) {
$("#relation-customer-id").val(ui.item.id);
  }',
	),
      'htmlOptions'=>array(
          'style'=>'width:100px;'
          ),
          ));
          echo CHtml::hiddenField('relation-customer-id','',array('id'=>'relation-customer-id'))
?>'s          
<div class="hint">Input id or part of the name.</div> 
<?php echo CHtml::textField('relation-text','',array('id'=>'relation-text',
          'style'=>'width:100px;'
          ));?>
          <?php
echo CHtml::button('OK',array(
          'onclick'=>'var customerId=$("#relation-customer-id").val();
          var relationText=$("#relation-text").val();
          if(customerId&&relationText){
$.post("'.Yii::app()->controller->createUrl('customerRelation/create').'",{
"CustomerRelation[fromCustomer]":"'.$customer->id.'",
"CustomerRelation[toCustomer]":customerId,
"CustomerRelation[relationText]":relationText
},
	function(){window.location.reload();})
	};return false;'
          )); ?>         
</div>
<?php
}

?>
